package edu.langnan.xiaoli.service.impl;

import edu.langnan.xiaoli.constant.RedisConstants;
import edu.langnan.xiaoli.constant.XiaoLiConstants;
import edu.langnan.xiaoli.core.domain.CarPoolDO;
import edu.langnan.xiaoli.core.dto.CarPoolDTO;
import edu.langnan.xiaoli.mapper.CarPoolMapper;
import edu.langnan.xiaoli.redis.RedisCache;
import edu.langnan.xiaoli.service.CarPoolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author cql
 */
@Service
public class CarPoolServiceImpl implements CarPoolService {

    @Autowired
    private CarPoolMapper carPoolMapper;

    @Autowired
    private RedisCache redisCache;

    @Override
    public void saveOrUpdate(CarPoolDO carPoolDO) {
        // TODO 如果目前正在评车，并且又要新增的化，那就返回错误信息
        /**
         * 如果带有 id,说明是修改
         * 如果没带有id,说明是新增
         */
        if (carPoolDO.getId() != null && carPoolDO.getId() != 0) {
            carPoolMapper.updateCarPool(carPoolDO);
        } else {
            carPoolDO.setStatus(XiaoLiConstants.PUBLISH_STATUS);
            carPoolMapper.insertCarPool(carPoolDO);
        }
        // TODO 可异步优化
        // 需要将数据存放到 redis 中,因为需要用到推荐功能
        redisCache.setCacheGeo(RedisConstants.CUR_CARING, carPoolDO.getId(), carPoolDO.getLatitude(), carPoolDO.getLongitude());
    }

    @Override
    public List<CarPoolDTO> carpoolList(String place, String status, Integer stuId) {
        // TODO 优化
        return carPoolMapper.carpoolList(place, status, stuId);
    }

    @Override
    public CarPoolDTO findCarPoolById(Integer carpoolId) {
        // TODO 优化
        return carPoolMapper.findCarPoolById(carpoolId);
    }

    @Override
    public List<CarPoolDTO> carHistory(Integer stuId) {
        return carPoolMapper.carpoolList("", XiaoLiConstants.EMPTY_STATUS, stuId);
    }

}
